package com.jiandaoshou;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.jiandaoshou.base.BaseActivity;
import com.jiandaoshou.http.APIDefine;
import com.jiandaoshou.http.Http;
import com.jiandaoshou.utils.ToastUtil;
import com.jiandaoshou.utils.UserUtils;

import java.util.HashMap;

import cn.smssdk.EventHandler;
import cn.smssdk.OnSendMessageHandler;
import cn.smssdk.SMSSDK;

public class YinHangKaDetailAct extends BaseActivity implements View.OnClickListener {
    //持卡人
    private EditText Edit_KaUserName;
    //卡号
    private EditText Edit_KaHao;
    //银行名称
    private TextView Text_YinHangName;
    //身份证号码
    private EditText Edit_ShenFenZhangNum;
    //手机号码
    private EditText Edit_PhoneNum;
    private EditText Edit_KaiHuHang;
    //获取银行的ID
    private String num_BankId;
    private String shopId;

    private EditText edt_code;
    private TextView tv_code;

    @Override
    public boolean showBackLogo() {
        return true;
    }

    @Override
    protected int getContentViewRes() {
        return R.layout.activity_yinhangka_detail;
    }

    @Override
    protected String getTitleText() {
        return "添加银行卡";
    }

    @Override
    public void init() {
        Edit_KaUserName = $(R.id.edit_ka_username);
        Edit_KaHao = $(R.id.edit_kahao);
        Text_YinHangName = $(R.id.edit_yinhang_name);
        Edit_ShenFenZhangNum = $(R.id.edit_shenfenzheng);
        Edit_PhoneNum = $(R.id.edit_phonenum);
        Edit_KaiHuHang = $(R.id.edit_kaihu);
        edt_code = $(R.id.et_code);
        tv_code = $(R.id.tv_code);
        tv_code.setOnClickListener(this);

        shopId = getIntent().getStringExtra("shopId");
        Edit_PhoneNum.setText(UserUtils.getShopPhone(this));

        cn.smssdk.SMSSDK.registerEventHandler(eh);
    }

    public void ChoiceYinHangKa(View view) {
        startActivityForResult(new Intent(YinHangKaDetailAct.this, ChoiceYinHangKaAct.class), 0);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_submit, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.submit:
                String phone = Edit_PhoneNum.getText().toString();
                String code = edt_code.getText().toString();

                String name = Edit_KaUserName.getText().toString();
                String kahao = Edit_KaHao.getText().toString();
                String yinhangmingzi = Text_YinHangName.getText().toString();
                String sfzh = Edit_ShenFenZhangNum.getText().toString();
                String kaihu = Edit_KaiHuHang.getText().toString();


                if (TextUtils.isEmpty(name)) {
                    Toast.makeText(YinHangKaDetailAct.this, "请输入持卡人姓名", Toast.LENGTH_SHORT).show();
                    return false;
                }

                if (TextUtils.isEmpty(kahao)) {
                    Toast.makeText(YinHangKaDetailAct.this, "请输入银行卡号", Toast.LENGTH_SHORT).show();
                    return false;
                }

                if (TextUtils.isEmpty(yinhangmingzi)) {
                    Toast.makeText(YinHangKaDetailAct.this, "请输入银行", Toast.LENGTH_SHORT).show();
                    return false;
                }

                if (TextUtils.isEmpty(kaihu)) {
                    Toast.makeText(YinHangKaDetailAct.this, "请输入开户行", Toast.LENGTH_SHORT).show();
                    return false;
                }

                if (TextUtils.isEmpty(sfzh)) {
                    Toast.makeText(YinHangKaDetailAct.this, "请输入您的身份证号", Toast.LENGTH_SHORT).show();
                    return false;
                }

                if (TextUtils.isEmpty(phone)) {
                    Toast.makeText(YinHangKaDetailAct.this, "请输入手机号", Toast.LENGTH_SHORT).show();
                    return false;
                }

                if (TextUtils.isEmpty(code)) {
                    Toast.makeText(YinHangKaDetailAct.this, "请输入验证码", Toast.LENGTH_SHORT).show();
                    return false;
                }
                Log.e("test", "onOptionsItemSelected: shopid:"+shopId);

                cn.smssdk.SMSSDK.submitVerificationCode("86", phone, code);

                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        switch (requestCode) {
            case 0:
                if (resultCode == 1) {
                    String bankname = data.getStringExtra("bankname");
                    num_BankId = data.getStringExtra("bank_id");
                    Text_YinHangName.setText(bankname);
                }

                break;
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_code:
                String phone = Edit_PhoneNum.getText().toString().trim();
                if (TextUtils.isEmpty(phone) == false && phone.length() == 11 && phone.startsWith("1")) {
                    cn.smssdk.SMSSDK.getVerificationCode("86", phone, new OnSendMessageHandler() {
                        @Override
                        public boolean onSendMessage(String s, String s1) {
                            handler.sendEmptyMessage(1);
                            return false;
                        }
                    });
                } else {
                    Toast.makeText(YinHangKaDetailAct.this, "请输入正确的手机号码！", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }

    private int time = 60;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
//                    cn.smssdk.SMSSDK.registerEventHandler(eh);
                    ToastUtil.toast("验证码发送成功");
                    Message me = Message.obtain();
                    time--;
                    me.what = 3;
                    sendMessageDelayed(me, 1000);
                    break;
                case 3:
                    if (time <= 0) {
                        tv_code.setEnabled(true);
                        tv_code.setText("获取验证码");
                    } else {
                        tv_code.setEnabled(false);
                        tv_code.setText(String.valueOf(time));
                    }
                    if (time >= 0) {
                        time--;
                        Message me1 = Message.obtain();
                        me1.what = 3;
                        sendMessageDelayed(me1, 1000);
                    }
                    break;
                case 2:
                    String str_CarHolderName = Edit_KaUserName.getText().toString();
                    String str_CardNum = Edit_KaHao.getText().toString();
                    String str_BankId = Text_YinHangName.getText().toString();
                    String str_ShenFenZhang = Edit_ShenFenZhangNum.getText().toString();
                    String str_MobileNumber = Edit_PhoneNum.getText().toString();
                    String str_kaihuhang = Edit_KaiHuHang.getText().toString();

                    HashMap<String, String> map = new HashMap<>();

                    map.put("strShopID", shopId);
                    map.put("strCardHolderName", str_CarHolderName);
                    map.put("strCardNumber", str_CardNum);
                    map.put("strBankID", num_BankId);
                    map.put("strIDCardNumber", str_ShenFenZhang);
                    map.put("strMobilePhoneNumber", str_MobileNumber);
                    map.put("strOpenBanKAccountAddress", str_kaihuhang);

                    Http.post(APIDefine.TF_Interface_IFCardInfo_SaveCard, map, new Http.HttpCallBack() {
                        @Override
                        public void onHttpSuccess(String response) throws Exception {
                            Toast.makeText(YinHangKaDetailAct.this, "提交成功", Toast.LENGTH_SHORT).show();
                            setResult(RESULT_OK);
                            YinHangKaDetailAct.this.finish();
                        }
                    });
                    break;
                case 7:
                    Toast.makeText(YinHangKaDetailAct.this, "验证失败", Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };

    private EventHandler eh = new EventHandler() {

        @Override
        public void onRegister() {
            super.onRegister();
            Log.e("test", "afterEvent:event 222222222222");
        }

        @Override
        public void beforeEvent(int i, Object o) {
            super.beforeEvent(i, o);
            Log.e("test", "afterEvent:event 1111111111111111111");
        }

        @Override
        public void afterEvent(int event, int result, Object data) {
            super.afterEvent(event, result, data);
            Log.e("test", "afterEvent:event " + event);
            //回调完成
            if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) {
                if (result == SMSSDK.RESULT_COMPLETE) {
                    //提交验证码成功
                    handler.sendEmptyMessage(2);
                } else {
                    handler.sendEmptyMessage(7);
                }

            } else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE) {
                //获取验证码成功
            } else if (event == SMSSDK.EVENT_GET_SUPPORTED_COUNTRIES) {
                //返回支持发送验证码的国家列表
            }
        }

        @Override
        public void onUnregister() {
            super.onUnregister();
        }
    };

}
